/*
 * File:    ClientContextCache.java
 * Created: 18-Jun-2006
 * Version: $Id: //depot/bitgate/bsse/src/bitgate/util/service/ClientContextCache.java#1 $
 *
 * COPYRIGHT (C) 2006, Bitgate Software, LLC.  All Rights Reserved.
 *
 * software@bitgatesoftware.com
 */

package com.bitgate.util.service.client;

import static com.bitgate.util.debug.Debug.*;

import java.util.HashMap;

/**
 * This is a centralized caching class that is used to store <code>ClientContext</code> information regarding connected
 * endpoint clients.  It stores the information regarding each client by its connection serial number, and can be retrieved
 * by that serial number for quick reference.
 *
 * @author Kenji Hollis &lt;kenji@bitgatesoftware.com&gt;
 * @version $Id: //depot/bitgate/bsse/src/bitgate/util/service/ClientContextCache.java#1 $
 */
public class ClientContextCache
{
    private final HashMap<String, ClientContext> clientContextCache;
    private static final ClientContextCache _default = new ClientContextCache();

    /**
     * Called internally - this sets up the cache.
     */
    private ClientContextCache()
    {
		this.clientContextCache = new HashMap<String, ClientContext>();
		
		if (isDebugEnabled()) {
			debug("Client Context Cache initialized.");
		}
    }

    /**
     * Returns the default instance of the cache.
     *
     * @return <code>static ClientContextCache</code> object.
     */
    public static ClientContextCache getDefault()
    {
    	return _default;
    }

    /**
     * Adds a <code>ClientContext</code> object by serial ID.
     *
     * @param serialId The serial ID of the connection.
     * @param cContext The <code>ClientContext</code> object to add.
     */
    public void add(long serialId, ClientContext cContext)
    {
		this.clientContextCache.put("" + serialId, cContext);
		
		if (isDebugEnabled()) {
			debug("Client context for serial '" + serialId + "' added.");
		}
    }

    /**
     * Retrieves a <code>ClientContext</code> object by serial ID.
     *
     * @param serialId The serial ID to look up.
     * @return <code>ClientContext</code> containing the result.
     */
    public ClientContext get(long serialId)
    {
    	return this.clientContextCache.get("" + serialId);
    }

    /**
     * Removes the client by serial ID.
     *
     * @param serialId The serial ID object to remove.
     */
    public void remove(long serialId)
    {
		this.clientContextCache.remove("" + serialId);
		
		if (isDebugEnabled()) {
			debug("Client context for serial '" + serialId + "' removed.");
		}
    }
}
